package com.tencent.crazypenguin.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/***
 *
 * <p>
 * Jpush工具类 <br>
 * </p>
 * @author julianxiao <br>
 * @since 1.0.0
 *
 */
public class JPushUtil {

    private static JPushClient jpushClient = null;

    static {
        jpushClient = new JPushClient(ConfigUtil.one().getMasterSecret(), ConfigUtil.one().getAppKey());
    }

    private JPushUtil(){}

    private static class JPushUtilGet{
        private static final JPushUtil instance= new JPushUtil();
    }

    public static JPushUtil one(){
        return JPushUtilGet.instance;
    }

    /***
     * 将消息推送给某台IOS设备
     * @param alias 设备别名
     * @param content 消息
     * @return 0:成功 / -1:失败
     */
    public int sendToIOS(String alias,String content){
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                                .addPlatformNotification(IosNotification.newBuilder()
                                        .setAlert(content)
                                        //.setSound("notification.wav")
                                        .build())
                                .build())
                .build();
        try {
            PushResult pushResult = jpushClient.sendPush(payload);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }
}
